package chances.epg.navigator.history;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class HistoryEntry implements java.io.Serializable {

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = -4420572528433418559L;
	private String path;
	private String queryString;
	private String ps;
	private String pi;
	
	public String getURL() {
		if (StringUtils.isBlank(this.queryString)) {
			return this.path;
		} else {
			return this.path + "?" + queryString;
		}
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public void appendParameter(String name, String value)  {
		//Pattern pattern = Pattern.compile(name + "=\\w*");
		try {
			value = java.net.URLEncoder.encode(value,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String param  = name+"="+ value;
		if(StringUtils.isNotEmpty(this.queryString)){
			//Matcher matcher = pattern.matcher(queryString);
			//if(matcher.find()){
			if(this.queryString.indexOf(name+"=")!=-1){
			    queryString = queryString.replaceAll("(" + name + "=[^&]*)", name + "=" + value);
			}else{
				queryString = queryString +"&"+param;
			}	
		}else{
			queryString = param;
		}
	}

	public String getPs() {
		return ps;
	}

	public String getPi() {
		return pi;
	}

	public void setPs(String ps) {
		this.ps = ps;
	}

	public void setPi(String pi) {
		this.pi = pi;
	}
}
